package com.tm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.dto.DishDTO;
import com.tm.entity.DishEntity;
import com.tm.mapper.DishMapper;
import com.tm.service.DishService;
import com.tm.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @PROJECT_NAME: tm-wm-syb
 * @DESCRIPTION:
 * @USER: DELL
 * @DATE: 2023/4/5 10:28
 */
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, DishEntity> implements DishService {
@Resource
    private DishMapper dishMapper;



    @Override
    public Page<DishEntity> pageList(Integer page, Integer pageSize, DishDTO dishDTO) {
        QueryWrapper<DishEntity> queryWrapper =new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(dishDTO.getName()),"a.name",dishDTO.getName());
        Page<DishEntity> page3 =new Page<>(page,pageSize);
        Page<DishEntity> page2 = dishMapper.page1(page3,queryWrapper);
       Integer count = dishMapper.selectCount(queryWrapper);
        page2.setTotal(count);
        return page2;
    }

    @Override
    public Boolean adddish(DishEntity dishEntity) {

        return dishMapper.insert(dishEntity) > 0;
    }

    @Override
    public R plqs(List<Long> collect) {
        QueryWrapper<DishEntity> wrapper = new QueryWrapper<>();
        wrapper.in("id",collect);
        List<DishEntity> list = dishMapper.selectList(wrapper);
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getStatus() != 1){
                list.get(i).setStatus(1);
                list.get(i).setUpdateTime(new Date());
                dishMapper.updateById(list.get(i));
            }else if(list.get(i).getStatus() != 0){
                list.get(i).setStatus(0);
                list.get(i).setUpdateTime(new Date());
                dishMapper.updateById(list.get(i));
            }
        }
        return R.success();

    }


}
